package com.xiaoyu.lanling.feature.view.weight.picker.single;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.feature.view.weight.picker.PickerModel;

import java.util.List;

public class PickerSingleBottomSheetDialog {

    private Context mContext;

    private BottomSheetDialog mBottomSheetDialog;
    private View mView;
    private TextView mCancelButton;
    private TextView mConfirmButton;
    private PickerSingleView mPickerSingleView;

    private OnPickResultListener mOnPickResultListener;

    public static PickerSingleBottomSheetDialog create(Context context) {
        return new PickerSingleBottomSheetDialog(context);
    }

    private PickerSingleBottomSheetDialog(Context context) {
        this.mContext = context;
        initView();
    }

    private void initView() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.dialog_picker_single, new FrameLayout(mContext));
        mCancelButton = mView.findViewById(R.id.cancel);
        mConfirmButton = mView.findViewById(R.id.ok);
        mPickerSingleView = mView.findViewById(R.id.picker_view);

        mCancelButton.setOnClickListener(v -> dismiss());
        mConfirmButton.setOnClickListener(v -> {
            if (mOnPickResultListener != null) {
                mOnPickResultListener.onDateResult(mPickerSingleView.getModel());
            }
            dismiss();
        });
    }

    public void initData(List<PickerModel> list) {
        mPickerSingleView.initData(list);
    }

    public PickerSingleBottomSheetDialog show() {
        if (mBottomSheetDialog == null) {
            if (mContext != null && mView != null) {
                mBottomSheetDialog = new BottomSheetDialog(mContext);
                mBottomSheetDialog.setContentView(mView);
                mBottomSheetDialog.show();
            }
        } else {
            mBottomSheetDialog.show();
        }
        return this;
    }

    public void dismiss() {
        mBottomSheetDialog.dismiss();
    }

    public PickerSingleBottomSheetDialog scrollToPosition(String key) {
        mPickerSingleView.scrollToPosition(key);
        return this;
    }

    public PickerSingleBottomSheetDialog scrollToPositionByValue(String value) {
        mPickerSingleView.scrollToPositionByValue(value);
        return this;
    }

    public PickerSingleBottomSheetDialog setOnPickResultListener(OnPickResultListener onPickResultListener) {
        this.mOnPickResultListener = onPickResultListener;
        return this;
    }

    public interface OnPickResultListener {
        void onDateResult(PickerModel model);
    }
}
